/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LaporanMingguan.java
 *
 * Created on 11 Nov 11, 22:38:12
 */
package MembuatLaporan;

import MenuUtamaSistem.Login;
import MenuUtamaSistem.MenuUtamaController;
import groovy.model.DefaultTableModel;
import groovy.model.ValueModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Ignacio
 */
public class LaporanMingguan extends javax.swing.JDialog {

    /** Creates new form LaporanMingguan */
    public LaporanMingguan() {
        initComponents();
        Login login = new Login();
        LaporanController.showDateAndClock(tanggalLabel, jamLabel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableLaporan = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        formLaporan = new javax.swing.JLabel();
        printButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        logoLabel = new javax.swing.JLabel();
        logo = new javax.swing.JLabel();
        waktuAwalChooser = new com.toedter.calendar.JDateChooser();
        waktuAkhirChooser = new com.toedter.calendar.JDateChooser();
        laporanButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jamLabel = new javax.swing.JLabel();
        tanggalLabel = new javax.swing.JLabel();
        batalButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        labaText = new javax.swing.JTextField();
        BG = new javax.swing.JLabel();

        setTitle("Laporan Keuangan - SP3EF");

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setLayout(null);

        tableLaporan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)},
                {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)},
                {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)},
                {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)}
            },
            new String [] {
                "TANGGAL", "NO TRANSAKSI", "JUMLAH PEMBELIAN", "JUMLAH PENJUALAN", "TOTAL HARGA PEMB", "TOTAL HARGA PENJ", "STOK PULSA"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableLaporan.setEnabled(false);
        tableLaporan.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tableLaporan);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(20, 210, 760, 190);

        jLabel1.setText("Waktu Awal");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 170, 80, 20);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 100, 1140, 10);

        jLabel2.setText("Waktu Akhir");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(280, 170, 80, 20);

        formLaporan.setFont(new java.awt.Font("Trebuchet MS", 1, 36));
        formLaporan.setText("Laporan Keuangan");
        jPanel1.add(formLaporan);
        formLaporan.setBounds(10, 20, 380, 50);

        printButton.setText("Cetak");
        printButton.setEnabled(false);
        printButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printButtonActionPerformed(evt);
            }
        });
        jPanel1.add(printButton);
        printButton.setBounds(640, 470, 130, 23);

        backButton.setText("Kembali Ke Menu Utama");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        jPanel1.add(backButton);
        backButton.setBounds(40, 470, 170, 23);

        logoLabel.setBackground(new java.awt.Color(153, 204, 255));
        logoLabel.setText("@MAFY com 2011");
        jPanel1.add(logoLabel);
        logoLabel.setBounds(360, 520, 170, 20);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MembuatLaporan/HP.png"))); // NOI18N
        jPanel1.add(logo);
        logo.setBounds(700, 0, 100, 100);
        jPanel1.add(waktuAwalChooser);
        waktuAwalChooser.setBounds(110, 170, 160, 20);
        jPanel1.add(waktuAkhirChooser);
        waktuAkhirChooser.setBounds(370, 170, 150, 20);

        laporanButton.setText("Buat Laporan");
        laporanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                laporanButtonActionPerformed(evt);
            }
        });
        jPanel1.add(laporanButton);
        laporanButton.setBounds(650, 170, 120, 23);

        jLabel3.setText("Jam");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(600, 130, 90, 20);

        jLabel4.setText("Tanggal");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(600, 110, 90, 20);
        jPanel1.add(jamLabel);
        jamLabel.setBounds(690, 130, 100, 20);
        jPanel1.add(tanggalLabel);
        tanggalLabel.setBounds(690, 110, 100, 20);

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });
        jPanel1.add(batalButton);
        batalButton.setBounds(530, 470, 90, 23);

        jLabel5.setText("Total Pemasukan");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(500, 410, 140, 20);
        jPanel1.add(labaText);
        labaText.setBounds(650, 410, 130, 20);

        BG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MenuUtamaSistem/BG2.jpg"))); // NOI18N
        jPanel1.add(BG);
        BG.setBounds(0, 0, 803, 560);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 803, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.getMain().setVisible(true);
    }//GEN-LAST:event_backButtonActionPerformed

    private void laporanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_laporanButtonActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            Laporan lapor = new Laporan();
            String format = "dd-MMM-yyyy";
            SimpleDateFormat formatDate = new SimpleDateFormat(format);
            lapor.setTanggalAwal(formatDate.format(waktuAwalChooser.getDate()));
            lapor.setTanggalAkhir(formatDate.format(waktuAkhirChooser.getDate()));
            List<Laporan> laporans = LaporanController.getAllDataJual(lapor.getTanggalAwal(), lapor.getTanggalAkhir());
            laporanTabelModel model = new laporanTabelModel(laporans);
            tableLaporan.setModel(model);
            long jual=0;
            long beli=0;
            for (int i = 0; i < laporans.size(); i++) {
                jual+=laporans.get(i).getHargaJual();
                beli+=laporans.get(i).getHargaBeli();
            }
            labaText.setText((jual-beli)+"");
            JOptionPane.showMessageDialog(null, "Laporan telah dibuat dengan "+tableLaporan.getRowCount()+" data");
            printButton.setEnabled(true);
        } catch (SQLException ex) {
            Logger.getLogger(LaporanMingguan.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "laporan gagal dibuat, tidak ada data");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LaporanMingguan.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage() + "");
        } catch (IOException ex) {
            Logger.getLogger(LaporanMingguan.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage() + "");
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(LaporanMingguan.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage() + "");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + "");
        }
        laporanButton.setEnabled(false);

    }//GEN-LAST:event_laporanButtonActionPerformed

    private void printButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printButtonActionPerformed
        // TODO add your handling code here:
        try {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("laba", Long.parseLong(labaText.getText()));
            System.out.println(""+labaText.getText());
            String source = "./Report/laporanMingguan.jasper";
//            JasperReport js= JasperCompileManager.compileReport(source);  
            JRTableModelDataSource jr = new JRTableModelDataSource(tableLaporan.getModel());           
            if (new File(source).exists() == false) {
                JOptionPane.showMessageDialog(null, "Please go to setting and Choose report Source");
                return;
            }
            JasperPrint jasperPrint = JasperFillManager.fillReport(source, param, jr);
            JasperViewer.viewReport(jasperPrint, false);
            for (int i = 0; i < tableLaporan.getRowCount(); i++) {
                String nomor = tableLaporan.getValueAt(i, 1).toString();
                if (nomor.contains("MAFYB")) {
                    LaporanController.ubahStatusPemb(nomor);
                } else if (nomor.contains("MAFYJ")) {
                    LaporanController.ubahStatusPenj(nomor);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("reports Error  " + e.toString());
        }
    }//GEN-LAST:event_printButtonActionPerformed

    private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
        // TODO add your handling code here:
        tableLaporan.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)},
                    {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)},
                    {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)},
                    {"-", "-", new Long(0), new Long(0), new Long(0), new Long(0), new Long(0)}
                },
                new String[]{
                    "TANGGAL", "NO TRANSAKSI", "JUMLAH PEMBELIAN", "JUMLAH PENJUALAN", "TOTAL HARGA PEMB", "TOTAL HARGA PENJ", "STOK PULSA"
                }) {

            Class[] types = new Class[]{
                java.lang.String.class, java.lang.String.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false, false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });

        laporanButton.setEnabled(true);
        waktuAwalChooser.setDate(null);
        waktuAkhirChooser.setDate(null);
        labaText.setText(null);
        printButton.setEnabled(false);
    }//GEN-LAST:event_batalButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LaporanMingguan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BG;
    private javax.swing.JButton backButton;
    private javax.swing.JButton batalButton;
    private javax.swing.JLabel formLaporan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel jamLabel;
    private javax.swing.JTextField labaText;
    private javax.swing.JButton laporanButton;
    private javax.swing.JLabel logo;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JButton printButton;
    private javax.swing.JTable tableLaporan;
    private javax.swing.JLabel tanggalLabel;
    private com.toedter.calendar.JDateChooser waktuAkhirChooser;
    private com.toedter.calendar.JDateChooser waktuAwalChooser;
    // End of variables declaration//GEN-END:variables
}
